package view.controller;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import lombok.SneakyThrows;
import utils.Connect;
import utils.Group;
import utils.User;
import view.GroupInformation;
import view.MainWindow;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
/**
 * @author
 */
public class GroupInformationController implements Initializable {

    @FXML
    public ImageView groupImageView;

    @FXML
    private ListView<Object> inviteFriendListView;

    @FXML
    private Label groupNameLabel;

    @FXML
    private Label groupAccountLabel;

    @FXML
    private Label grouperLabel;

    @FXML
    private ListView<Object> groupMemberListView;


    @Override
    public void initialize(URL location, ResourceBundle resources) {

        Group group= GroupInformation.group;

        //加载群主页
        Image image=new Image( String.format("file:photo/%s.png", group.getGroupAccount()));
        groupImageView.setImage(image);
        groupAccountLabel.setText(group.getGroupAccount());
        groupNameLabel.setText(group.getGroupName());

        //加载群成员列表
        ObservableList<Object> groupMember1= FXCollections.observableArrayList();
        groupMemberListView.setItems(groupMember1);
        List<User> groupMember=null;
        if(group.getMembers()!=null){
            groupMember=group.getMembers();
            for(User x:groupMember)
            {
                ImageView imageView = new ImageView("file:photo/"+x.getAccount()+".png");
                imageView.setFitHeight(50);
                imageView.setFitWidth(50);
                Label label=new Label(x.getName());
                HBox hBox=new HBox();
                hBox.getChildren().addAll(imageView,label);
                groupMember1.add(hBox);
            }
        }
        //加载邀请好友列表

        ObservableList<Object> myFriends1= FXCollections.observableArrayList();
        inviteFriendListView.setItems(myFriends1);
        List<User> myFriends= MainWindow.getMainWindow().getFriends();
        List<User>  inviteFriends=new ArrayList<>();
        inviteFriends.addAll(myFriends);
        if(groupMember!=null) {
            inviteFriends.removeAll(groupMember);
        }
        for(User x:inviteFriends)
        {
            ImageView imageView = new ImageView("file:photo/"+x.getAccount()+".png");
            imageView.setFitHeight(50);
            imageView.setFitWidth(50);
            Label label=new Label(x.getName());
            Button button=new Button("邀请");
            HBox hBox=new HBox();
            hBox.getChildren().addAll(imageView,label,button);
            myFriends1.add(hBox);
            button.setOnAction(new EventHandler<ActionEvent>() {
                @SneakyThrows
                @Override
                public void handle(ActionEvent event) {
                    myFriends1.remove(hBox);
                    group.getMembers().add(x);
                    Connect.getConnect().userAccessToGroup(x,group);

                    ImageView imageView = new ImageView("file:photo/"+x.getAccount()+".png");
                    imageView.setFitHeight(50);
                    imageView.setFitWidth(50);
                    Label label=new Label(x.getName());
                    HBox hBox=new HBox();
                    hBox.getChildren().addAll(imageView,label);
                    groupMember1.add(hBox);
                }
            });
        }
    }
}
